package com.dream.inspect.modules.app.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dream.inspect.common.utils.Query;
import com.dream.inspect.common.utils.R;
import com.dream.inspect.modules.business.dao.SuperviseImageDao;
import com.dream.inspect.modules.business.dao.SuperviseQuestionDao;
import com.dream.inspect.modules.business.dao.SuperviseRecordDao;
import com.dream.inspect.modules.dream.pojo.SuperviseRecordFormMap;

@Controller
@RequestMapping("/app/backup")
public class BackupController{

    @Autowired
    SuperviseRecordDao superviseRecordDao;
    @Autowired
    SuperviseQuestionDao superviseQuestionDao;
    @Autowired
    SuperviseImageDao superviseImageDao;


    /**
     *
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/getBackupList")
    public R getBackupList(@RequestParam Map<String, Object> params){
        /*if(StringUtil.isEmpty(params.get("isOverbue").toString())){
            params.put("isOverbue",3);
        }*/
        params.put("sidx","CREATE_TIME");
        params.put("overbueTime",plusDay(3,new Date()));
        Page<SuperviseRecordFormMap> page = selectThisPage(new Query<SuperviseRecordFormMap>(params),
                new Query<SuperviseRecordFormMap>(params).getPage()
        );

        return R.ok().put("data",page.getRecords()).put("count",page.getTotal());

    }


    /**
     * 通过id删除备存数据
     * @param userId
     * @param rId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delBackupOverbueById")
    public R delBackupOverbueById(String userId,String rId){
        superviseRecordDao.delBackupDatas(userId,rId,null);
        return R.ok().put("data","操作成功");
    }

    /**
     * 清除所有备存
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delBackupAll")
    public R delBackupAll(String userId){
        superviseRecordDao.delBackupDatas(userId,null,null);
        return R.ok().put("data","操作成功");
    }

    /**
     * 清除所有过期
     * @param userId
     * @param flag
     * @return
     */
    @ResponseBody
    @RequestMapping("/delBackupOverbueAll")
    public R delBackupOverbueAll(String userId,String flag){
        superviseRecordDao.delBackupDatas(userId,null,"0");
        return R.ok().put("data","操作成功");
    }


    @ResponseBody
    @RequestMapping("/getBackupDataById")
    public R getBackupDataById(String userId,String rId){

        Map<String,Object> ss = superviseRecordDao.getRecordByBackuoId(userId,rId);

        /*List<SupervisionQuestionBean> sq = superviseQuestionDao.getQuestionByRrcordId(rId,userId);
        for (SupervisionQuestionBean q : sq) {
            List<SuperviseImageEntity> imageList = superviseImageDao.getImageListByWid(q.getId());
            q.setSuperviseImageEntityList(imageList);
        }
        ss.setSupervisionQuestionBeanList(sq);*/
        System.out.println(ss.get("id"));
        System.out.println(ss.get("jsonRecord"));
//        ss.put("jsonRecord",JsonUtil.objToJson(ss.get("jsonRecord")));
//        System.out.println(ss.get("jsonRecord"));
        return R.ok().put("data",ss);
    }






    private Page<SuperviseRecordFormMap> selectThisPage(Map<String ,Object> map, Page<SuperviseRecordFormMap> page) {
        page.setRecords(superviseRecordDao.findBackupList(map,page));
        return page;
    }

    public String plusDay(int num, Date d){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currdate = format.format(d);
        System.out.println("现在的日期是：" + currdate);
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DATE, ca.get(Calendar.DATE) - num);
        String enddate = format.format(ca.getTime());
        System.out.println("增加天数以后的日期：" + enddate);
        return enddate;
    }
}
